package com.yanqu.road.server.gameplayer.module.activity.langyabang;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.args.LangYaBuildingCoinArgs;
import com.yanqu.road.entity.activity.langyabang.config.LangYaBangConfig;
import com.yanqu.road.entity.activity.langyabang.config.LangYaCaseConfig;
import com.yanqu.road.entity.activity.langyabang.config.LangYaCaseEventConfig;
import com.yanqu.road.entity.activity.langyabang.data.LangYaBangWarReportData;
import com.yanqu.road.entity.activity.langyabang.data.LangYaUserCaseGameData;
import com.yanqu.road.entity.activity.langyabang.entity.LangYaTypePatrons;
import com.yanqu.road.entity.activity.langyabang.entity.LangYaUserPatronsInfo;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.enums.activity.langyabang.eLangYaNoticeType;
import com.yanqu.road.entity.log.langyabang.LogLangYaBangCaseGameFinish;
import com.yanqu.road.entity.log.langyabang.LogLangYaBangPve;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.logic.bussiness.activity.LangYaBangBusiness;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.pb.CrossRankPb;
import com.yanqu.road.logic.pb.PatronsPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.LangYaBangProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.langyabang.LangYaBangMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.WorldMessageMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.logic.pb.LangYaBangPb;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


public class LangYaBangModule extends GeneralModule {

    private LangYaUserCaseGameData userCaseGameData;

    private Property addPowerUseProperty;

    private Property declareWarUseProperty;

    private List<LangYaBangWarReportData> userWarReportData = new ArrayList<>();

    private List<LangYaBangWarReportData> needInsertUserWarReportData = new ArrayList<>();

    public LangYaBangModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        ActivityInfo activityInfo = LangYaBangMgr.getActivityInfo();
        if (ActivityMgr.activityInShowTime(activityInfo)){
            userWarReportData = LangYaBangBusiness.getLangYaBangWarReportData(activityInfo.getActivityId(),getUserId(),LangYaBangMgr.getMaxNoticeAndReportNum());
            userWarReportData.sort(Comparator.comparing(LangYaBangWarReportData::getTime));
            userCaseGameData = LangYaBangBusiness.getLangYaUserCaseGameData(activityInfo.getActivityId(),getUserId());
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if (userCaseGameData!=null){
            if (userCaseGameData.isInsertOption()){
                LangYaBangBusiness.addLangYaUserCaseGameData(userCaseGameData);
            }else if (userCaseGameData.isUpdateOption()){
                LangYaBangBusiness.updateLangYaUserCaseGameData(userCaseGameData);
            }
        }
        for (LangYaBangWarReportData userWarReportData:needInsertUserWarReportData){
            if (userWarReportData.isInsertOption()){
                LangYaBangBusiness.addLangYaBangWarReportData(userWarReportData);
            }
        }
        needInsertUserWarReportData.clear();
        return true;
    }

    @Override
    public void afterLogin() {
        reloadActivity();
    }

    /**
     * 上传个人信息
     */
    private void updateUserToCross(int activityId) {
        LangYaBangProto.LoginSendToCrossMsg.Builder builder = LangYaBangProto.LoginSendToCrossMsg.newBuilder();
        builder.setUserBaseInfo(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_LOGIN_SEND_DATA_TO_CROSS, builder, player.getUserId(), activityId, groupId);
    }

    /**
     * 同步配置
     */
    private void syncConfig() {
        if (LangYaBangMgr.getActivityInfo() == null) {
            return;
        }
        if (LangYaBangMgr.getConfig() == null){
            return;
        }
        LangYaBangProto.LangYaBangConfigSyncMsg.Builder configMsg = LangYaBangMgr.getConfigMsg(player.getLanguage());
        player.sendPacket(ClientProtocol.U_LANG_YA_BANG_CONFIG_SYNC, configMsg);
    }

    /**
     * 同步金陵风云
     *
     * @param activityId
     */

    private void syncUserCaseData(int activityId) {
        if (userCaseGameData == null || userCaseGameData.getActivityId() != activityId) {
            if (LangYaBangMgr.getConfig() == null){
                return;
            }
            LangYaUserCaseGameData tmpLangYaUserCaseGameData = new LangYaUserCaseGameData();
            tmpLangYaUserCaseGameData.setActivityId(activityId);
            tmpLangYaUserCaseGameData.setUserId(getUserId());

            Map<Integer, Integer> initCaseGameMap = LangYaBangMgr.getInitCaseGameMap();
            if (initCaseGameMap.isEmpty()){
                log.error("琅琊榜活动{},金陵风云配置不存在,暂不初始化",activityId);
                return;
            }
            tmpLangYaUserCaseGameData.setAllCaseData(initCaseGameMap);

            tmpLangYaUserCaseGameData.setCaseGameStatus(0);
            tmpLangYaUserCaseGameData.setCaseGameNowCase(0);
            tmpLangYaUserCaseGameData.setCaseGameNowCard(0);
            tmpLangYaUserCaseGameData.setCaseGameNowValue(new ArrayList<>());
            userCaseGameData = tmpLangYaUserCaseGameData;
            userCaseGameData.setInsertOption();
        }
        if (userCaseGameData != null){
            LangYaBangProto.LangYaBangCaseUserDataSyncMsg.Builder builder = LangYaBangPb.parseCaseGameData(userCaseGameData);
            player.sendPacket(ClientProtocol.U_LANG_YA_BANG_USER_CASE_DATA_SYNC, builder);
        }
    }


    /**
     * 同步主玩法
     *
     * @param activityId
     * @return
     */

    private int syncUserMainData(int activityId, boolean update) {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.LangYaBang.getValue())){
            return GameErrorCode.E_LANG_YA_BANG_SYSTEM_NO_OPEN;
        }
        ActivityInfo activityInfo = LangYaBangMgr.getActivityInfo();
        if (activityInfo == null) {
            return GameErrorCode.E_LANG_YA_BANG_ACTIVITY_NO_OPEN;
        }
        if (activityInfo.getActivityId() != activityId) {
            return GameErrorCode.E_LANG_YA_BANG_ACTIVITY_ID_ERROR;
        }
        if (!ActivityMgr.activityInTime(activityInfo)) {
            return GameErrorCode.E_LANG_YA_BANG_ACTIVITY_NO_OPEN;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityInfo.getActivityId(), getUserId());
        if (groupId == UnionActivityGroupHelper.NO_GROUP_ID) {
            return GameErrorCode.E_LANG_YA_BANG_NO_GROUP;
        }
        getUserDataFromCross(activityId, true, update);
        return 0;
    }

    public void initSystem() {
        reloadActivity();

    }

    public void reloadActivity(){
        ActivityInfo activityInfo = LangYaBangMgr.getActivityInfo();
        if (activityInfo != null) {
            if (ActivityMgr.activityInShowTime(activityInfo)) {
                syncConfig();
                int activityId = activityInfo.getActivityId();
                updateUserToCross(activityId);
                if (SystemOpenMgr.systemOpen(player, eSystemId.LangYaBang.getValue())) {
                    syncUserCaseData(activityId);
                    syncUserMainData(activityId, true);
                }
            }
        }
    }


    /**
     * 检测
     *
     * @param activityId
     * @return
     */
    public int simpleCheck(int activityId) {

        if (!SystemOpenMgr.systemOpen(player, eSystemId.LangYaBang.getValue())){
            return GameErrorCode.E_LANG_YA_BANG_SYSTEM_NO_OPEN;
        }
        ActivityInfo activityInfo = LangYaBangMgr.getActivityInfo();
        if (activityInfo == null) {
            return GameErrorCode.E_LANG_YA_BANG_ACTIVITY_NO_OPEN;
        }
        if (activityInfo.getActivityId() != activityId) {
            return GameErrorCode.E_LANG_YA_BANG_ACTIVITY_ID_ERROR;
        }
        if (!ActivityMgr.activityInTime(activityInfo)) {
            return GameErrorCode.E_LANG_YA_BANG_ACTIVITY_NO_OPEN;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityInfo.getActivityId(), getUserId());
        if (groupId == UnionActivityGroupHelper.NO_GROUP_ID) {
            return GameErrorCode.E_LANG_YA_BANG_NO_GROUP;
        }
        return 0;
    }

    /**
     * 检测
     *
     * @param activityId
     * @return
     */
    public int caseCheck(int activityId) {

        if (!SystemOpenMgr.systemOpen(player, eSystemId.LangYaBang.getValue())){
            return GameErrorCode.E_LANG_YA_BANG_SYSTEM_NO_OPEN;
        }
        ActivityInfo activityInfo = LangYaBangMgr.getActivityInfo();
        if (activityInfo == null) {
            return GameErrorCode.E_LANG_YA_BANG_ACTIVITY_NO_OPEN;
        }
        if (activityInfo.getActivityId() != activityId) {
            return GameErrorCode.E_LANG_YA_BANG_ACTIVITY_ID_ERROR;
        }
        //展示期间也能玩
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            return GameErrorCode.E_LANG_YA_BANG_ACTIVITY_NO_OPEN;
        }
        return 0;
    }

    /**
     * 去跨服拿个人数据
     *
     * @param activityId
     * @param isSync
     */

    public int getUserDataFromCross(int activityId, boolean isSync, boolean isUpdate) {
        LangYaBangProto.LangYaBangGetUserDataFromCrossReqMsg.Builder toCrossBuilder = LangYaBangProto.LangYaBangGetUserDataFromCrossReqMsg.newBuilder();
        ActivityInfo activityInfo = LangYaBangMgr.getActivityInfo();
        if (activityInfo == null){
            return GameErrorCode.E_LANG_YA_BANG_ACTIVITY_NO_OPEN;
        }
        if (!ActivityMgr.activityInTime(activityInfo)){
            return GameErrorCode.E_LANG_YA_BANG_CROSS_DATA_NO_INIT;
        }

        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, getUserId());
        if (groupId == UnionActivityGroupHelper.NO_GROUP_ID) {
            if (!isSync) {
                return GameErrorCode.E_LANG_YA_BANG_NO_GROUP;
            }
        }
        toCrossBuilder.setActivityId(activityId);
        toCrossBuilder.setIsSync(isSync);
        if (isUpdate) {
            Map<Integer, UserPatrons> typeBestPatron = getTypeBestPatron();
            if (typeBestPatron == null || typeBestPatron.size() != 5){
                return GameErrorCode.E_LANG_YA_BANG_SYSTEM_NO_OPEN;
            }
            List<LangYaBangProto.LangYaUserPatronsInfo> patornInfoMsgList = LangYaBangPb.getUpdatePatronInfoMsgList(typeBestPatron);
            toCrossBuilder.addAllNewPatronsInfo(patornInfoMsgList);
        }
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_BANG_GET_AND_UPDATE_DATA, toCrossBuilder, player.getUserId(), activityId, groupId);
        return 0;
    }

    /**
     *   获取各职业最强门客
     */
    public Map<Integer,UserPatrons> getTypeBestPatron(){
        Map<Integer, UserPatrons> newPatronsInfoMap = new ConcurrentHashMap<>();
        for (eOccupationType occupation : eOccupationType.values()) {
            short occupationType = occupation.getValue();
            UserPatrons userPatron = player.getModule(PatronsModule.class).getMaxEarnPatronsByType(occupationType);
            if (userPatron == null){
                log.error("琅琊榜 {} 存在玩家{}拥有资格但却没有五个不同类型门客 无法进入游戏",LangYaBangMgr.getActivityInfo().getActivityId(),getUserId());
                return newPatronsInfoMap;
            }
            newPatronsInfoMap.put((int)occupationType,userPatron);
        }
        return newPatronsInfoMap;
    }




    /**
     * 去跨服摇骰子
     *
     * @param langYaRollDiceReqMsg
     * @return
     */

    public int rollDice(LangYaBangProto.LangYaRollDiceReqMsg langYaRollDiceReqMsg) {
        int activityId = langYaRollDiceReqMsg.getActivityId();
        int check = simpleCheck(activityId);
        if (check != 0) {
            return check;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_BANG_ROLL_DICE, langYaRollDiceReqMsg.toBuilder(), player.getUserId(), activityId, groupId);
        return 0;
    }

    /**
     * 跨服摇骰子回来处理
     *
     * @param langYaRollDiceRespMsg
     */

    public void dealDiceResult(LangYaBangProto.LangYaRollDiceRespMsg langYaRollDiceRespMsg) {
//        使用跨服的历史获得金币来更新 不用这个了
//        long totalCoinReward = 0;
//        if (langYaRollDiceRespMsg.hasCoinReward()) {
//            totalCoinReward += langYaRollDiceRespMsg.getCoinReward();
//        }
//        if (langYaRollDiceRespMsg.hasHighCoinReward()) {
//            totalCoinReward += langYaRollDiceRespMsg.getHighCoinReward();
//        }
//
//        totalCoinReward = totalCoinReward * langYaRollDiceRespMsg.getRewardAddition();
        int scoreItemId = LangYaBangMgr.getScoreItemId();
        int totalScoreReward = langYaRollDiceRespMsg.getRewardAddition() * LangYaBangMgr.getScorePerPower();
        if (totalScoreReward > 0 && scoreItemId != 0){
            Property property = new Property(scoreItemId,totalScoreReward);
            player.getModule(CurrencyModule.class).addCurrency(property,eLogMoneyType.LangYaBang,eLogMoneyType.LangYaBangRollScoreAdd);
        }

        if (langYaRollDiceRespMsg.hasInspirationReward()){
            player.notifyListener(eGamePlayerEventType.LangYaBangInspirationCondition.getValue(),langYaRollDiceRespMsg.getInspirationReward());
            Property property = checkAutoSendInspirationConditionReward();
            if (!property.isNothing()){
                player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.LangYaBang,eLogMoneyType.LangYaBangCondition);
                LangYaBangProto.LangYaRollDiceRespMsg.Builder builder = langYaRollDiceRespMsg.toBuilder();
                builder.setInspirationConditionReward(PropertyHelper.parsePropertyToString(property));
                langYaRollDiceRespMsg = builder.build();
            }
        }


        int buildingTotalLevel = LangYaBangMgr.getBuildingTotalLevel(langYaRollDiceRespMsg.getUserData().getBuildingDataList());

        player.notifyListener(eGamePlayerEventType.LangYaBuildingLevelCondition.getValue(),buildingTotalLevel);
        player.notifyListener(eGamePlayerEventType.LangYaUserBuildingCoinRank.getValue(),new LangYaBuildingCoinArgs(buildingTotalLevel,langYaRollDiceRespMsg.getUserData().getHistoryCoin()));

        player.sendPacket(ClientProtocol.U_LANG_YA_BANG_ROLL_DICE, langYaRollDiceRespMsg.toBuilder());
    }

    /**
     * 跨服pve返回
     */
    public void dealPveResult(LangYaBangProto.DoPveEventRespMsg langYaPveRespMsg) {

        if (langYaPveRespMsg.getIsEventOver() == 1){
            LangYaBangWarReportData warReportData = new LangYaBangWarReportData();
            warReportData.setActivityId(LangYaBangMgr.getActivityInfo().getActivityId());
            warReportData.setUserId(player.getUserId());
            warReportData.setOtherUserId(langYaPveRespMsg.getGameEventData().getTargetId());
            if (langYaPveRespMsg.getGameEventData().getNpcNowBlood() < langYaPveRespMsg.getGameEventData().getNpcAllBlood()){
                warReportData.setOperateType(eLangYaNoticeType.pveLose.getValue());
            }else {
                warReportData.setOperateType(eLangYaNoticeType.pveWin.getValue());
            }
            if (langYaPveRespMsg.hasFinalReward()){
                String finalReward = langYaPveRespMsg.getFinalReward();
                Property reward = PropertyHelper.parseStringToProperty(finalReward);
                reward.goodCountMultiNum(langYaPveRespMsg.getRewardPowerAddition());
                player.getModule(CurrencyModule.class).addCurrency(reward,eLogMoneyType.LangYaBang,eLogMoneyType.LangYaBangPveReward);
                warReportData.setGoodReward(PropertyHelper.parsePropertyToString(reward));
            }
            if (langYaPveRespMsg.hasFinalRewardCoin()){
                warReportData.setCoinReward(langYaPveRespMsg.getFinalRewardCoin()*langYaPveRespMsg.getRewardPowerAddition());
            }
            if (langYaPveRespMsg.getGameEventData().hasUserInfo()){
                warReportData.setOtherData(LangYaBangPb.parseUserBaseInfoToBriefData(PlayerBasePb.parseToUserBaseInfo(langYaPveRespMsg.getGameEventData().getUserInfo())));
            }
            warReportData.setTime(System.currentTimeMillis());
            addWarReportData(warReportData);
            player.notifyListener(eGamePlayerEventType.LangYaUserBuildingCoinRank.getValue(),new LangYaBuildingCoinArgs(0,langYaPveRespMsg.getUserData().getHistoryCoin()));

        }

        player.sendPacket(ClientProtocol.U_LANG_YA_BANG_PVE, langYaPveRespMsg.toBuilder());
    }


    /**
     * 去跨服代政
     */
    public int daiZheng(LangYaBangProto.DoDaiZhengEventReqMsg reqMsg) {
        int activityId = reqMsg.getActivityId();
        int check = simpleCheck(activityId);
        if (check != 0) {
            return check;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_BANG_DAI_ZHENG, reqMsg.toBuilder(), player.getUserId(), activityId, groupId);
        return 0;
    }

    public void dealChooseDeclareWarUser(LangYaBangProto.ChooseDeclareWarBattleUserResp resp){
        int scoreItemId = LangYaBangMgr.getScoreItemId();
        int scorePerPower = LangYaBangMgr.getScorePerPower();
        int resultScore = scorePerPower *LangYaBangMgr.getDeclareWarBattleEnergyUsed();
        if (resultScore > 0 && scoreItemId != 0){
            Property property = new Property(scoreItemId,resultScore);
            player.getModule(CurrencyModule.class).addCurrency(property,eLogMoneyType.LangYaBang,eLogMoneyType.LangYaBangRollScoreAdd);
        }
        player.sendPacket(ClientProtocol.U_LANG_YA_DECLARE_WAR_CHOOSE_USER,resp.toBuilder());
    }

    public void dealDaizhengResult(LangYaBangProto.DoDaizhengEventRespMsg respMsg) {
        if (respMsg.getIsOver() == 1){
            LangYaBangWarReportData warReportData = new LangYaBangWarReportData();
            warReportData.setActivityId(LangYaBangMgr.getActivityInfo().getActivityId());
            warReportData.setUserId(player.getUserId());
            warReportData.setOtherUserId(respMsg.getGameEventData().getTargetId());
            warReportData.setOperateType(eLangYaNoticeType.daiZhengOther.getValue());
            warReportData.setGoodReward("");
            if (respMsg.hasResultReward()){
                String resultReward = respMsg.getResultReward();
                if (!StringUtils.isNullOrEmpty(resultReward)){
                    if (!resultReward.equals("0")){
                        Property property = PropertyHelper.parseStringToProperty(resultReward);
                        property.goodCountMultiNum(respMsg.getRewardPowerAddition());
                        player.getModule(CurrencyModule.class).addCurrency(property,eLogMoneyType.LangYaBang,eLogMoneyType.LangYaBangDaiZhengReward);
                        warReportData.setGoodReward(PropertyHelper.parsePropertyToString(property));
                    }
                }
            }
            if (respMsg.hasCoinReward()){

                warReportData.setCoinReward(respMsg.getCoinReward()*respMsg.getRewardPowerAddition());
            }
            if (respMsg.getGameEventData().hasUserInfo()){
                warReportData.setOtherData(LangYaBangPb.parseUserBaseInfoToBriefData(PlayerBasePb.parseToUserBaseInfo(respMsg.getGameEventData().getUserInfo())));
            }
            warReportData.setOperateTime(respMsg.getGameEventData().getWinTime());
            warReportData.setTime(System.currentTimeMillis());
            addWarReportData(warReportData);
            player.notifyListener(eGamePlayerEventType.LangYaUserBuildingCoinRank.getValue(),new LangYaBuildingCoinArgs(0,respMsg.getUserData().getHistoryCoin()));
        }
        player.sendPacket(ClientProtocol.U_LANG_YA_BANG_DAI_ZHENG, respMsg.toBuilder());
    }

    /**
     * 金陵风云开始游戏案件
     */
    public int openLangYaBangCaseGame(int activityId, int chooseCase) {
        int check = caseCheck(activityId);
        if (check != 0) {
            return check;
        }
        if (userCaseGameData == null || userCaseGameData.getActivityId() != activityId){
            syncUserCaseData(activityId);
        }
        if (userCaseGameData == null){
            return GameErrorCode.E_LANG_YA_BANG_CROSS_DATA_NO_INIT;
        }

        if(!userCaseGameData.getAllCaseData().containsKey(chooseCase)){
            log.error("琅琊榜活动{},金陵风云{}配置不存在",activityId,chooseCase);
            return GameErrorCode.E_LANG_YA_BANG_CONFIG_ERROR;
        }
        LangYaCaseConfig langYaCaseConfig = LangYaBangMgr.getLangYaCaseConfig(chooseCase);
        if (langYaCaseConfig == null){
            log.error("琅琊榜活动{},金陵风云{}配置不存在",activityId,chooseCase);
            return GameErrorCode.E_LANG_YA_BANG_CONFIG_ERROR;
        }
        List<Integer> initNum = langYaCaseConfig.getInitNum();
        if (initNum.size() == 0 || initNum.size() > 4){
            return GameErrorCode.E_LANG_YA_BANG_CONFIG_ERROR;
        }


        if (userCaseGameData.getAllCaseData().get(chooseCase) == 0) {    //扣物品 加**
            int itemId = LangYaBangMgr.getConfig().getCaseClueItemId();
            int caseClueCost = LangYaBangMgr.getConfig().getCaseClueCost();
            Property costProperty = new Property(itemId,caseClueCost);
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(costProperty)){
                return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            }
            player.getModule(CurrencyModule.class).removeCurrency(costProperty,eLogMoneyType.LangYaBang,eLogMoneyType.LangYaBangCaseGameUnlock);
            userCaseGameData.getAllCaseData().put(chooseCase, 1);
        }

        userCaseGameData.setCaseGameNowCase(chooseCase);
        userCaseGameData.setCaseGameNowValue(new ArrayList<>(initNum));

        userCaseGameData.setCaseGameStatus(1);
        userCaseGameData.setCaseGameNowCard(langYaCaseConfig.getInitEvent());


        LangYaBangProto.LangYaBangCaseGameStartRespMsg.Builder builder = LangYaBangProto.LangYaBangCaseGameStartRespMsg.newBuilder();
        builder.setRet(0);
        builder.setData(LangYaBangPb.parseCaseGameData(userCaseGameData));
        player.sendPacket(ClientProtocol.U_LANG_YA_BANG_CASE_GAME_START, builder);
        return 0;
    }

    /**
     * 金陵风云快速扫荡
     *
     * @param activityId
     * @param caseId
     * @param times
     * @return
     */
    public int quickLangYaCaseGame(int activityId, int caseId, int times) {
        int check = caseCheck(activityId);
        if (check != 0) {
            return check;
        }
        LangYaCaseConfig langYaCaseConfig = LangYaBangMgr.getLangYaCaseConfig(caseId);
        String reward = langYaCaseConfig.getReward();
        Property property = PropertyHelper.parseStringToProperty(reward);
        property.goodCountMultiNum(times);
        player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.LangYaBang, eLogMoneyType.LangYaBangQuickCaseGame);

        LangYaBangProto.LangYaBangQuickCaseGameRespMsg.Builder builder = LangYaBangProto.LangYaBangQuickCaseGameRespMsg.newBuilder();
        builder.setRet(0);
        builder.setReward(PropertyHelper.parsePropertyToString(property));
        player.sendPacket(ClientProtocol.U_LANG_YA_BANG_QUICK_CASE_GAME, builder);
        return 0;
    }

    public int operateCaseGameCard(int activityId, int chooseId) {
        LangYaBangProto.LangYaBangUserDealCaseGameRespMsg.Builder builder = LangYaBangProto.LangYaBangUserDealCaseGameRespMsg.newBuilder();
        int check = caseCheck(activityId);
        if (check != 0) {
            return check;
        }
        if (userCaseGameData.getCaseGameStatus() != 1) {
            return GameErrorCode.E_LANG_YA_BANG_NO_OPEN_CASE_GAME;
        }

        builder.setRet(0);
        int caseGameNowEvent = userCaseGameData.getCaseGameNowCase();
        int caseGameNowCard = userCaseGameData.getCaseGameNowCard();

        LangYaCaseConfig langYaCaseConfig = LangYaBangMgr.getLangYaCaseConfig(caseGameNowEvent);
        LangYaCaseEventConfig langYaCaseEventConfig = LangYaBangMgr.getLangYaCaseEventConfig(caseGameNowEvent, caseGameNowCard);
        int nextCard = 0;
        if (chooseId == 0) {
            calculateValue(userCaseGameData, langYaCaseEventConfig.getEffectA());
            nextCard = langYaCaseEventConfig.getJumpA();
        } else {
            calculateValue(userCaseGameData, langYaCaseEventConfig.getEffectB());
            nextCard = langYaCaseEventConfig.getJumpB();
        }

        int overType = checkCaseGameOver(userCaseGameData, langYaCaseConfig.getLoseNum1(), langYaCaseConfig.getLoseNum2());
        if (overType > 0) {

            builder.setIsOver(1);
            builder.setOverType(overType);
            userCaseGameData.setCaseGameStatus(0);
            builder.setUserData(LangYaBangPb.parseCaseGameData(userCaseGameData));
            player.sendPacket(ClientProtocol.U_LANG_YA_BANG_CASE_GAME_DEAL, builder);
            userCaseGameData.resetGame();
            syncUserCaseData(activityId);
            int passType = 2;       //再次游玩失败为2;
            //还没通关过的话 改为0
            if (userCaseGameData.getAllCaseData().get(caseGameNowEvent) != 2){
                passType = 0;
            }
            //日志
            LogLangYaBangCaseGameFinish logLangYaBangCaseGameFinish = new LogLangYaBangCaseGameFinish(activityId,getUserId(),caseGameNowEvent,passType,"",new Date());
            AutoLogMgr.add(logLangYaBangCaseGameFinish);
            return 0;
        } else {
            if (nextCard == 0) {

                String reward = "";
                int passType = 3;       //3为通关后再次游玩成功
                //第一次通?
                if (userCaseGameData.getAllCaseData().get(caseGameNowEvent) != 2){
                    passType = 1;       //首次游玩成功
                    userCaseGameData.getAllCaseData().put(caseGameNowEvent, 2);
                    Property property = new Property();
                    reward = langYaCaseConfig.getReward();
                    if (!StringUtils.isNullOrEmpty(reward) && !reward.equals("0")){
                        property = PropertyHelper.parseStringToProperty(reward);
                        player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.LangYaBang, eLogMoneyType.LangYaBangCaseGame);
                    }
                    player.notifyListener(eGamePlayerEventType.LangYaCaseCondition.getValue(),1);
                    Property conditionReward = checkAutoSendCaseGameConditionReward();
                    if (conditionReward != null){
                        if (!conditionReward.isNothing()){
                            property.addProperty(conditionReward);
                        }
                    }
                    reward = PropertyHelper.parsePropertyToString(property);
                    builder.setOverReward(reward);

                }


                builder.setIsOver(1);
                builder.setOverType(0);
                userCaseGameData.setCaseGameStatus(0);
                builder.setUserData(LangYaBangPb.parseCaseGameData(userCaseGameData));
                player.sendPacket(ClientProtocol.U_LANG_YA_BANG_CASE_GAME_DEAL, builder);
                //日志
                LogLangYaBangCaseGameFinish logLangYaBangCaseGameFinish = new LogLangYaBangCaseGameFinish(activityId,getUserId(),caseGameNowEvent,passType,reward,new Date());
                AutoLogMgr.add(logLangYaBangCaseGameFinish);

                userCaseGameData.resetGame();
                syncUserCaseData(activityId);
                return 0;
            } else {
                builder.setIsOver(0);
                builder.setOverType(0);
                userCaseGameData.setCaseGameNowCard(nextCard);

                builder.setUserData(LangYaBangPb.parseCaseGameData(userCaseGameData));
                player.sendPacket(ClientProtocol.U_LANG_YA_BANG_CASE_GAME_DEAL, builder);
                return 0;
            }

        }
    }

    public int checkCaseGameOver(LangYaUserCaseGameData userCaseGameData, List<Integer> loseList, List<Integer> loseList2) {

        List<Integer> caseGameNowValue = userCaseGameData.getCaseGameNowValue();
        for (int i = 0; i < caseGameNowValue.size(); i++) {
            if (caseGameNowValue.get(i) < loseList.get(i) || caseGameNowValue.get(i) > loseList2.get(i)) {
                return i + 1;
            }
        }
        return 0;
    }

    public void calculateValue(LangYaUserCaseGameData userCaseGameData, List<Integer> addList) {
        List<Integer> caseGameNowValue = userCaseGameData.getCaseGameNowValue();
        for (int i = 0; i < addList.size(); i++) {
            caseGameNowValue.set(i, caseGameNowValue.get(i) + addList.get(i));
        }
        userCaseGameData.setUpdateOption();
    }

    public int dealPveEvent(LangYaBangProto.DoPveEventReqMsg msg) {
        int check = simpleCheck(msg.getActivityId());
        if (check != 0) {
            return check;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(msg.getActivityId(), player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_BANG_DEAL_PVE_EVENT, msg.toBuilder(), player.getUserId(), msg.getActivityId(), groupId);
        return 0;
    }

    public int dealPvpEvent(LangYaBangProto.DoPvpEventReqMsg msg) {
        int check = simpleCheck(msg.getActivityId());
        if (check != 0) {
            return check;
        }
        if (msg.getChooseBuildingType() <= 0 || msg.getChooseBuildingType() > 5) {
            return GameErrorCode.E_LANG_YA_BANG_PVP_CHOOSE_ERROR_BUILDING;
        }

        int groupId = GameUnionActivityGroupMgr.getGroupId(msg.getActivityId(), player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_BANG_DEAL_PVP_EVENT, msg.toBuilder(), player.getUserId(), msg.getActivityId(), groupId);
        return 0;
    }

    /**
     * 跨服pvp返回
     */
    public void dealPvpResult(LangYaBangProto.DoPvpEventRespMsg langYaPvpRespMsg) {
        if (langYaPvpRespMsg.getIsEventOver()==1) {
            long userScore = langYaPvpRespMsg.getUserData().getUserScore();
            player.notifyListener(eGamePlayerEventType.LangYaUnionScoreRank.getValue(), new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, BigInteger.valueOf(userScore), player.getUserInfo().getUnionUid()));
            player.notifyListener(eGamePlayerEventType.LangYaUserBuildingCoinRank.getValue(),new LangYaBuildingCoinArgs(0,langYaPvpRespMsg.getUserData().getHistoryCoin()));
            LangYaBangWarReportData warReportData = new LangYaBangWarReportData();
            warReportData.setActivityId(LangYaBangMgr.getActivityInfo().getActivityId());
            warReportData.setUserId(player.getUserId());
            warReportData.setOtherUserId(langYaPvpRespMsg.getGameEventData().getTargetId());
            if (langYaPvpRespMsg.getGameEventData().getWinTime()>0){
                warReportData.setOperateType(eLangYaNoticeType.attackOtherWin.getValue());
            }else {
                warReportData.setOperateType(eLangYaNoticeType.attackOtherLose.getValue());
            }
            if (langYaPvpRespMsg.hasReward()){
                String resultReward = langYaPvpRespMsg.getReward();
                Property reward = PropertyHelper.parseStringToProperty(resultReward);
                reward.goodCountMultiNum(langYaPvpRespMsg.getRewardPowerAddition());
                player.getModule(CurrencyModule.class).addCurrency(reward,eLogMoneyType.LangYaBang,eLogMoneyType.LangYaBangPvpReward);
                warReportData.setGoodReward(PropertyHelper.parsePropertyToString(reward));
            }
            if (langYaPvpRespMsg.hasCoinReward()){
                warReportData.setCoinReward(langYaPvpRespMsg.getCoinReward()*langYaPvpRespMsg.getRewardPowerAddition());
            }
            if (langYaPvpRespMsg.getGameEventData().hasUserInfo()){
                warReportData.setOtherData(LangYaBangPb.parseUserBaseInfoToBriefData(PlayerBasePb.parseToUserBaseInfo(langYaPvpRespMsg.getGameEventData().getUserInfo())));
            }
            if (langYaPvpRespMsg.hasScoreReward()){
                warReportData.setScoreChange(langYaPvpRespMsg.getOtherLoseScore());
            }
            warReportData.setTime(System.currentTimeMillis());
            warReportData.setOperateTime(langYaPvpRespMsg.getGameEventData().getWinTime());
            warReportData.setSelfScoreChange(langYaPvpRespMsg.getScoreReward() * langYaPvpRespMsg.getRewardPowerAddition());
            addWarReportData(warReportData);
        }
        player.sendPacket(ClientProtocol.U_LANG_YA_BANG_PVP, langYaPvpRespMsg.toBuilder());
    }

    /**
     * 加体力
     */
    public int addPower(LangYaBangProto.LangYaBangUsePowerItemReqMsg msg) {
        int check = simpleCheck(msg.getActivityId());
        if (check != 0) {
            return check;
        }
        LangYaBangConfig config = LangYaBangMgr.getConfig();
        int energyItemId = config.getEnergyItemId();
        Property costProperty = new Property(energyItemId, BigInteger.valueOf(msg.getUseNum()));
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(costProperty)) { //物品不足
            return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
        }
        //防止玩家没回包就瞎点
        if (addPowerUseProperty != null){
            return GameErrorCode.E_BAG_ITEM_CAN_NO_USE;
        }
        player.getModule(PreReduceModule.class).preReduce(costProperty);
        addPowerUseProperty = costProperty;
        int groupId = GameUnionActivityGroupMgr.getGroupId(msg.getActivityId(), player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_ADD_POWER, msg.toBuilder(), player.getUserId(), msg.getActivityId(), groupId);
        return 0;
    }

    /**
     * 加体力返回
     */
    public void dealAddPower(LangYaBangProto.LangYaBangUsePowerItemRespMsg msg) {
        Property needCostProperty = addPowerUseProperty;
        if (addPowerUseProperty != null) {
            // 恢复预扣除项
            player.getModule(PreReduceModule.class).restorePreReduce(addPowerUseProperty);
            addPowerUseProperty = null;
        }
        if (msg.getRet() == 0) {
            CurrencyModule module = player.getModule(CurrencyModule.class);
            boolean removeSuccess = module.removeCurrency(needCostProperty, eLogMoneyType.LangYaBang, eLogMoneyType.LangYaBangAddPowerUse);
            if (!removeSuccess) {
                log.info("玩家: {},在琅琊榜活动id{}中加体力的道具{}不足,只能全部扣除", getUserId(), LangYaBangMgr.getActivityInfo().getActivityId(), PropertyHelper.parsePropertyToString(needCostProperty));
                for (int goodsId : needCostProperty.getGoods().keySet()) {
                    UserBagItem userBagItem = player.getModule(BagModule.class).getUserBagItem(goodsId);
                    if (userBagItem != null) {
                        userBagItem.setNum(0);
                    }
                }
            }
        }
        player.sendPacket(ClientProtocol.U_LANG_YA_BANG_ADD_POWER, msg.toBuilder());
    }

    /**
     * 升级建筑
     *
     * @param reqMsg
     * @return
     */
    public int upgradeBuilding(LangYaBangProto.LangYaBangUpgradeBuildingReqMsg reqMsg) {
        int check = simpleCheck(reqMsg.getActivityId());
        if (check != 0) {
            return check;
        }
        if (reqMsg.getBuildingType() <= 0 || reqMsg.getBuildingType() > 5) {
            return GameErrorCode.E_LANG_YA_BANG_UPDATE_BUILDING_TYPE_NO_EXIST;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(reqMsg.getActivityId(), player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_UPGRADE_BUILDING, reqMsg.toBuilder(), player.getUserId(), reqMsg.getActivityId(), groupId);
        return 0;
    }

    /**
     * 解决升级建筑的结果
     *
     * @param respMsg
     */
    public void dealUpgradeBuildingResult(LangYaBangProto.LangYaBangUpgradeBuildingRespMsg respMsg) {
        //先算总等级
        List<LangYaBangProto.LangYaUserBuildingData> buildingDataList = respMsg.getUserData().getBuildingDataList();
        int buildingTotalLevel = LangYaBangMgr.getBuildingTotalLevel(buildingDataList);
        player.notifyListener(eGamePlayerEventType.LangYaBuildingLevelCondition.getValue(),buildingTotalLevel);
        player.notifyListener(eGamePlayerEventType.LangYaUserBuildingCoinRank.getValue(),new LangYaBuildingCoinArgs(buildingTotalLevel,respMsg.getUserData().getHistoryCoin()));

        if (respMsg.getUpdateBuilding().hasOperateStatus()){
            if (respMsg.getUpdateBuilding().getOperateStatus() == 1){
                LangYaBangWarReportData warReportData = new LangYaBangWarReportData();
                warReportData.setActivityId(LangYaBangMgr.getActivityInfo().getActivityId());
                warReportData.setUserId(player.getUserId());
                warReportData.setOtherUserId(respMsg.getUpdateBuilding().getLevelId());
                warReportData.setOperateType(eLangYaNoticeType.upgradeBuilding.getValue());
                warReportData.setOperateTime(1);
                warReportData.setGoodReward("");
                warReportData.setCoinReward(0);
                warReportData.setScoreChange(0);
                warReportData.setCostCoin(respMsg.getCostCoin());
                warReportData.setTime(System.currentTimeMillis());

                if (respMsg.hasReward()){
                    Property property = PropertyHelper.parseStringToProperty(respMsg.getReward());
                    player.getModule(CurrencyModule.class).addCurrency(property,eLogMoneyType.LangYaBang,eLogMoneyType.LangYaBangUpgradeBuilding);
                    warReportData.setGoodReward(respMsg.getReward());
                }

                addWarReportData(warReportData);
            }
        }

        player.sendPacket(ClientProtocol.U_LANG_YA_BANG_UPGRADE_BUILDING, respMsg.toBuilder());
    }

    /**
     * 宣战
     *
     * @param reqMsg
     * @return
     */
    public int declareWar(LangYaBangProto.InitiateDeclareWarReqMsg reqMsg) {
        int check = simpleCheck(reqMsg.getActivityId());
        if (check != 0) {
            return check;
        }
        if (StringUtils.isNullOrEmpty(reqMsg.getUnionId())) {
            return GameErrorCode.E_LANG_YA_BANG_UNION_NO_EXIST;
        }
        //防止没回包就乱点
        if (declareWarUseProperty != null){
            return GameErrorCode.E_BAG_ITEM_CAN_NO_USE;
        }
        int tokenId = LangYaBangMgr.getConfig().getTokenId();
        int tempTokenId = LangYaBangMgr.getConfig().getTemporaryTokenId();
        int needToken = LangYaBangMgr.getConfig().getDeclareWarTokenUsed();
        //先判断有没有临时
        Property needUseItem = new Property(tempTokenId, needToken);
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(needUseItem)) { //物品不足
            needUseItem = new Property(tokenId,needToken);
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(needUseItem)){
                return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            }
        }
        player.getModule(PreReduceModule.class).preReduce(needUseItem);
        declareWarUseProperty = needUseItem;

        int groupId = GameUnionActivityGroupMgr.getGroupId(reqMsg.getActivityId(), player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_DECLARE_WAR, reqMsg.toBuilder(), player.getUserId(), reqMsg.getActivityId(), groupId);
        return 0;
    }

    /**
     * 处理宣战结果
     *
     * @param respMsg
     */
    public void dealDeclareWarResult(LangYaBangProto.InitiateDeclareWarRespMsg respMsg) {
        Property needCostProperty = declareWarUseProperty;
        if (needCostProperty != null) {
            // 恢复预扣除项
            player.getModule(PreReduceModule.class).restorePreReduce(needCostProperty);
            declareWarUseProperty = null;
        }
        if (respMsg.getRet() == 0) {
            CurrencyModule module = player.getModule(CurrencyModule.class);
            boolean removeSuccess = module.removeCurrency(needCostProperty, eLogMoneyType.LangYaBang, eLogMoneyType.LangYaBangDeclareWarCost);
            if (!removeSuccess) {
                log.info("玩家: {},在琅琊榜活动id{}中宣战道具{}不足,只能全部扣除", getUserId(), LangYaBangMgr.getActivityInfo().getActivityId(), PropertyHelper.parsePropertyToString(needCostProperty));
                for (int goodsId : needCostProperty.getGoods().keySet()) {
                    UserBagItem userBagItem = player.getModule(BagModule.class).getUserBagItem(goodsId);
                    if (userBagItem != null) {
                        userBagItem.setNum(0);
                    }
                }
            }


            LangYaBangProto.LangYaDeclareWarNoticeData noticeData = respMsg.getNoticeData();
            WorldMessageProto.WorldMessageTempMsg.Builder chatMsg = WorldMessageProto.WorldMessageTempMsg.newBuilder();
            chatMsg.setActivityId(LangYaBangMgr.getActivityInfo().getActivityId());

            chatMsg.setSendTime(DateHelper.getCurrentSecond());
            chatMsg.setUserId(getUserId());
            UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(getUserId(), GameServer.getInstance().getServerId());
            if (userBaseInfo != null) {
                chatMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            }
            String content = LangYaBangPb.parseNoticeChatString(noticeData);
            chatMsg.setContent(content);
            //发活动去
            chatMsg.setType(eMessageType.LangYaBangDeclareWarInActivity.getValue());
            WorldMessageMgr.sendCrossActivityChat(chatMsg);
            //发商会去
            chatMsg.setType(eMessageType.LangYaBangDeclareWar.getValue());
            WorldMessageMgr.sendUnionChat(player, chatMsg);

            player.sendPacket(ClientProtocol.U_LANG_YA_BANG_DECLARE_WAR, respMsg.toBuilder());
            return;
        }
        player.sendPacket(ClientProtocol.U_LANG_YA_BANG_DECLARE_WAR, respMsg.toBuilder());
    }

    /**
     * 获取宣战列表
     */
    public int getLangYaBangGetSimpleDeclareWarInfo(LangYaBangProto.GetHistorySimpleDeclareWarInfoReqMsg msg) {
        int check = simpleCheck(msg.getActivityId());
        if (check != 0) {
            return check;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(msg.getActivityId(), player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_GET_DECLARE_WAR_SIMPLE, msg.toBuilder(), player.getUserId(), msg.getActivityId(), groupId);
        return 0;
    }

    public int getLangYaBangGetDeclareWarDetail(LangYaBangProto.GetHistoryDeclareWarDetailInfoReqMsg msg) {
        int check = simpleCheck(msg.getActivityId());
        if (check != 0) {
            return check;
        }
        if (msg.getDeclareWarUid() < 0) {
            return GameErrorCode.E_LANG_YA_BANG_DECLARE_WAR_ID_NO_EXIST;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(msg.getActivityId(), player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_GET_DECLARE_WAR_DETAIL, msg.toBuilder(), player.getUserId(), msg.getActivityId(), groupId);
        return 0;
    }

    public int getLangYaBangCanDeclareWarUnionList(LangYaBangProto.GetLangYaDeclareWarUnionListReq req) {
        int check = simpleCheck(req.getActivityId());
        if (check != 0) {
            return check;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(req.getActivityId(), player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_GET_CAN_DECLARE_UNION_LIST, req.toBuilder(), player.getUserId(), req.getActivityId(), groupId);
        return 0;
    }

    public Property checkAutoSendInspirationConditionReward(){
        ActivityInfo activityInfo = LangYaBangMgr.getActivityInfo();
        if (activityInfo == null){
            return null;
        }
        BaseActivityData baseActivityData = player.getModule(ActivityModule.class).getBaseActivityData(activityInfo.getActivityId());
        Property totalReward = new Property();
        if(baseActivityData != null){

            for (BaseActivityCondition baseActivityCondition : baseActivityData.getBaseActivityConditionList()) {
                if(baseActivityCondition.getConditionType() == eGamePlayerEventType.LangYaBangInspirationCondition.getValue()){
                    if (!baseActivityCondition.isCompleted(player)){        //未完成
                        continue;
                    }
                    if (baseActivityCondition.isGetReward()){               //已经领奖
                        continue;
                    }
                    String reward = baseActivityCondition.getReward();
                    totalReward.addProperty(PropertyHelper.parseStringToProperty(reward));
                    baseActivityData.finish(baseActivityCondition.getConditionId());
                    player.getModule(ActivityModule.class).onActivityDataChanged(baseActivityData);
                }
            }
        }
        return totalReward;
    }

    public int chooseDeclareWarEnemyUser(LangYaBangProto.ChooseDeclareWarBattleUserReq req){
        int check = simpleCheck(req.getActivityId());
        if (check != 0) {
            return check;
        }
        if (req.getDeclareUid()<0){
            return GameErrorCode.E_LANG_YA_BANG_DECLARE_WAR_ID_NO_EXIST;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(req.getActivityId(), player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_BANG_CHOOSE_DECLARE_WAR_BATTLE_USER, req.toBuilder(), player.getUserId(), req.getActivityId(), groupId);
        return 0;
    }

    public int getUserWarReportData(int activityId,int type){
        int check = simpleCheck(activityId);
        if (check != 0) {
            return check;
        }
        if (type < 0 || type >12){
            return GameErrorCode.E_LANG_YA_BANG_WAR_REPORT_TYPE_ERROR;
        }
        LangYaBangProto.LangYaBangGetAllWarReportRespMsg.Builder builder = LangYaBangProto.LangYaBangGetAllWarReportRespMsg.newBuilder();
        if (type == 0){
            for(LangYaBangWarReportData warReportData:userWarReportData){
                if (activityId == warReportData.getActivityId()){
                    builder.addNotice(LangYaBangPb.parseNoticeMsg(warReportData));
                }
            }
        } else {
            for(LangYaBangWarReportData warReportData:userWarReportData){
                if (activityId == warReportData.getActivityId()){
                    if (warReportData.getOperateType() == type){
                        if (warReportData.getTime() > userCaseGameData.getHasReadTime()){
                            builder.addNotice(LangYaBangPb.parseNoticeMsg(warReportData));
                        }
                    }
                }
            }
        }
        userCaseGameData.setHasReadTime(System.currentTimeMillis());
        builder.setRet(0);
        player.sendPacket(ClientProtocol.U_LANG_YA_BANG_GET_WAR_REPORT,builder);
        return 0;
    }

    public void addWarReportData(LangYaBangWarReportData data){
        data.setInsertOption();
        needInsertUserWarReportData.add(data);
        if (userWarReportData.size() >= LangYaBangMgr.getMaxNoticeAndReportNum()){
            userWarReportData.remove(0);
        }
        userWarReportData.add(data);
    }

    public void syncWarReportToUser(LangYaBangWarReportData data){
        userCaseGameData.setHasReadTime(System.currentTimeMillis());
        LangYaBangProto.LangYaWarReportSyncMsg.Builder sync = LangYaBangProto.LangYaWarReportSyncMsg.newBuilder();
        LangYaBangProto.LangYaNoticeData.Builder builder = LangYaBangPb.parseNoticeMsg(data);
        sync.setUserId(data.getUserId());
        sync.setActivityId(data.getActivityId());
        sync.setData(builder);
        player.sendPacket(ClientProtocol.U_LANG_YA_BANG_NOTICE_USER,sync);
    }

    public int getOtherHelpReward (LangYaBangProto.LangYaGetOtherHelpRewardReqMsg reqMsg){
        int check = simpleCheck(reqMsg.getActivityId());
        if (check != 0) {
            return check;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(reqMsg.getActivityId(), player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_BANG_GET_OTHER_HELP_REWARD, reqMsg.toBuilder(), player.getUserId(), reqMsg.getActivityId(), groupId);
        return 0;
    }

    public void dealOtherHelpRewardResult(LangYaBangProto.LangYaGetOtherHelpRewardRespMsg respMsg){
        LangYaBangProto.LangYaGetOtherHelpRewardRespMsg.Builder builder = respMsg.toBuilder();
        Property property = new Property();
        if (respMsg.hasGoodsReward()){
            String goodsReward = respMsg.getGoodsReward();
            property = PropertyHelper.parseStringToProperty(goodsReward);
            player.getModule(CurrencyModule.class).addCurrency(property,eLogMoneyType.LangYaBang,eLogMoneyType.LangYaBangGetOtherHelpReward);
        }
        LangYaBangConfig config = LangYaBangMgr.getConfig();
        if (config!=null){
            property.addProperty(config.getCoinItemId(),BigInteger.valueOf(builder.getCoinReward()));
            builder.setGoodsReward(PropertyHelper.parsePropertyToString(property));
        }

        player.sendPacket(ClientProtocol.U_LANG_YA_GET_OTHER_HELP_REWARD,builder);
    }

    public int getAllDeclareWarNotice(LangYaBangProto.LangYaGetDeclareWarAllNoticeReqMsg msg){
        int check = simpleCheck(msg.getActivityId());
        if (check != 0) {
            return check;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(msg.getActivityId(), player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_BANG_GET_ALL_DECLARE_NOTICE, msg.toBuilder(), player.getUserId(), msg.getActivityId(), groupId);
        return 0;
    }

    public int getNoEndDeclareWarList(LangYaBangProto.LangYaBangGetNoOverDeclareWarListReqMsg reqMsg){
        int check = simpleCheck(reqMsg.getActivityId());
        if (check != 0) {
            return check;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(reqMsg.getActivityId(), player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_BANG_GET_NO_END_DECLARE_WAR, reqMsg.toBuilder(), player.getUserId(), reqMsg.getActivityId(), groupId);
        return 0;
    }

    public Property checkAutoSendCaseGameConditionReward(){
        ActivityInfo activityInfo = LangYaBangMgr.getActivityInfo();
        if (activityInfo == null){
            return null;
        }
        BaseActivityData baseActivityData = player.getModule(ActivityModule.class).getBaseActivityData(activityInfo.getActivityId());
        Property totalReward = new Property();
        if(baseActivityData != null){
            for (BaseActivityCondition baseActivityCondition : baseActivityData.getBaseActivityConditionList()) {
                if(baseActivityCondition.getConditionType() == eGamePlayerEventType.LangYaCaseCondition.getValue()){
                    if (!baseActivityCondition.isCompleted(player)){        //未完成
                        continue;
                    }
                    if (baseActivityCondition.isGetReward()){               //已经领奖
                        continue;
                    }
                    String reward = baseActivityCondition.getReward();
                    totalReward.addProperty(PropertyHelper.parseStringToProperty(reward));
                    baseActivityData.finish(baseActivityCondition.getConditionId());
                    player.getModule(ActivityModule.class).onActivityDataChanged(baseActivityData);
                }
            }
        }
        return totalReward;
    }

    public int setAutoSwitch(LangYaBangProto.LangYaBangSetAutoSwitchReqMsg reqMsg){
        int simpleCheck = simpleCheck(reqMsg.getActivityId());
        if (simpleCheck != 0){
            return simpleCheck;
        }
        if (reqMsg.getOpenSwitchTypeList().isEmpty()){
            return GameErrorCode.E_LANG_YA_BANG_SWITCH_TYPE_ERROR;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(reqMsg.getActivityId(), player.getUserId());

        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_LANG_YA_CROSS_UPDATE_AUTO_SWITCH, reqMsg.toBuilder(), player.getUserId(), reqMsg.getActivityId(), groupId);
        return 0;
    }



}
